package com.tiger.project.service.impl;

import java.time.LocalDate;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.tiger.common.core.redis.RedisCache;
import com.tiger.common.utils.Code;
import com.tiger.common.utils.DateUtils;
import com.tiger.common.utils.StringUtils;
import com.tiger.project.domain.ConstructionApply;
import com.tiger.project.domain.ConstructionEnclosure;
import com.tiger.project.domain.MaintenanceEnclosure;
import com.tiger.project.domain.ProjectArchive;
import com.tiger.project.domain.req.ConstructionApplyBack;
import com.tiger.project.mapper.ConstructionApplyMapper;
import com.tiger.project.mapper.ConstructionEnclosureMapper;
import com.tiger.project.mapper.ProjectArchiveMapper;
import com.tiger.project.service.IConstructionApplyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 【建设项目】Service业务层处理
 *
 * @author standard
 * @date 2022-09-16
 */
@Service
public class ConstructionApplyServiceImpl implements IConstructionApplyService {
    @Autowired
    private ConstructionApplyMapper constructionApplyMapper;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private ConstructionEnclosureMapper enclosureMapper;
    @Autowired
    private ProjectArchiveMapper projectArchiveMapper;

    /**
     * 查询【建设项目】
     *
     * @param id 【建设项目】主键
     * @return 【建设项目】
     */
    @Override
    public ConstructionApply selectConstructionApplyById(Long id) {
        ConstructionApply constructionApply = constructionApplyMapper.selectConstructionApplyById(id);

        //附件
        ConstructionEnclosure enclosure = new ConstructionEnclosure();
        enclosure.setConstructionApplyId(id);
        List<ConstructionEnclosure> constructionEnclosureList = enclosureMapper.selectConstructionEnclosureList(enclosure);
        if (constructionEnclosureList !=null && constructionEnclosureList.size() > 0){
            constructionApply.getEnclosureList().addAll(constructionEnclosureList);
        }

        return constructionApply;
    }

    /**
     * 查询【建设项目】列表
     *
     * @param constructionApply 【建设项目】
     * @return 【建设项目】
     */
    @Override
    public List<ConstructionApply> selectConstructionApplyList(ConstructionApply constructionApply) {
        return constructionApplyMapper.selectConstructionApplyList(constructionApply);
    }

    /**
     * 新增【建设项目】
     *
     * @param constructionApply 【建设项目】
     * @return 结果
     */
    @Override
    public int insertConstructionApply(ConstructionApply constructionApply) {
        int code = redisCache.getCacheObjectCode(LocalDate.now().format(Code.fmt) + "xm");
        addCode(code);
        constructionApply.setConstructionApplyNo(String.format("%s%s%04d", "XM",LocalDate.now().format(Code.fmt), code));
        constructionApply.setCreatedAt(DateUtils.getNowDate());
        constructionApply.setIsDeleted(0L);
        constructionApply.setStatus(1L);
        int i = constructionApplyMapper.insertConstructionApply(constructionApply);
        change(constructionApply);
        return i;
    }

    /**
     * 修改【建设项目】
     *
     * @param constructionApply 【建设项目】
     * @return 结果
     */
    @Override
    public int updateConstructionApply(ConstructionApply constructionApply) {
        change(constructionApply);
        constructionApply.setUpdatedAt(DateUtils.getNowDate());
        return constructionApplyMapper.updateConstructionApply(constructionApply);
    }

    /**
     * 批量删除【建设项目】
     *
     * @param ids 需要删除的【建设项目】主键
     * @return 结果
     */
    @Override
    public int deleteConstructionApplyByIds(Long[] ids) {
        return constructionApplyMapper.deleteConstructionApplyByIds(ids);
    }

    /**
     * 删除【建设项目】信息
     *
     * @param id 【建设项目】主键
     * @return 结果
     */
    @Override
    public int deleteConstructionApplyById(Long id) {
        return constructionApplyMapper.deleteConstructionApplyById(id);
    }

    //审核回调
    @Override
    public int callBack(ConstructionApplyBack constructionApplyBack) {
        //审核通过,项目可归档
        ConstructionApply constructionApply = constructionApplyMapper.selectConstructionApplyById(constructionApplyBack.getId());
        BeanUtils.copyProperties(constructionApplyBack,constructionApply);
        if (constructionApplyBack.getStatus() == 3L){
            ProjectArchive archive = new ProjectArchive();
            archive.setProjectId(constructionApply.getId());
            archive.setProjectNo(constructionApply.getConstructionApplyNo());
            archive.setProjectType(2L);
            archive.setCreatedAt(DateUtils.getNowDate());
            archive.setIsDeleted(0L);
            archive.setProjectName(constructionApply.getName());
            archive.setProjectDate(constructionApply.getReportingDate());
            archive.setBudgetAmount(constructionApply.getBudgetAmount());
            archive.setIsArchived(0L);
            archive.setCompanyId(constructionApply.getCompanyId());
            projectArchiveMapper.insertProjectArchive(archive);
        }

        constructionApply.setUpdatedAt(DateUtils.getNowDate());
        return constructionApplyMapper.updateConstructionApply(constructionApply);
    }

    /**
     * 生成编码
     */
    private synchronized void addCode(int code) {
        redisCache.setCacheObject(LocalDate.now().format(Code.fmt) + "xm", code + 1);
    }

    private void change(ConstructionApply constructionApply){
        enclosureMapper.deleteConstructionEnclosureByConstructionId(constructionApply.getId());
        if (StringUtils.isNotEmpty(constructionApply.getEnclosureList())){
            constructionApply.getEnclosureList().forEach(item ->{
                item.setCreatedAt(DateUtils.getNowDate());
                item.setState("1");//上传成功
                item.setConstructionApplyId(constructionApply.getId());
                enclosureMapper.insertConstructionEnclosure(item);
            });
        }
    }


}
